package com.bxait.toymail.order.config;

import cn.hutool.json.JSONObject;
import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.domain.AlipayTradePayModel;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.bxait.toymail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/*
@description:
@ClassName AlipayTemplate
@author Chen
@create 2023-05-18 0:05
@Version 1.0
*/
@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000120615517";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCEZldrALrybtrkm4VRXd5tjA+TJqFz2zJrcTmlwGjmieSvBRyY2TEkS6oaEWEWTaY8e4HE1ZF+IZ9XdLcmfJORejd0QoEvBSV5+gZ528VudrD9ITVz+SKmVQaQKjaxqZ7D8FGyLL3a5bnOFf2hFu3OX3Si1+CMrEdSyMWgk+SICttUsjiC6SstvPljKzW2JVA5T/3sseEeu3yCFeOVgPt7RTBbO3qL54b+nRMCa2l3DICfItG6Wf3IXI7LG4vj6j/MxRxWhEQ7IgDl3uNqVC4HHQTWz2kCxGp88ZSbC1zScyHUAgsxKPQrDpJpiCWn2YJwJK5AKRbN2knC2KaIg2YTAgMBAAECggEAE2sfLyZwRNZVbGoS55go41BDiIWF8IQZpQB6QAE0YXuf+rt5MkHIn1YpoKEj6C2DUCOdKM1aBXHQ+Pa5Vf+3dQLD111ZrRfII1e0U3MURdtKohWYzWWg21xlFgfa9bu2+W3pzYQaZFyFH+DBzjqarkX5yVh4Kqmp3E5uDdsjDYC0DmNrcq0s2ErNmBhwnLNkf5WxSkkl3vlx6oRL1LAH21wEx6285ntJ01JcNCzYwWGkQnXRpaYpQnJF7ef/WhwErl9Jllft2nx9NVlPzaNsGmNpb/3xZd7D2kTr9TQgVq4q53PuAV3rVopAMEFSwqR9gUSCbBaAGo67TgdtVGkBwQKBgQDCBOSIS8b/0c9tfgHVpJQSPvwJHCvYEUnaZiBnmnLBlSFnMAo7QS1V8Sm+eIeP7DUjMrxlfxQZxy1rApSz70vf9Z/jH0muLyI4zP36SUSS0p6prYOq7j6pFzXUW9/VSM/lr7x7bNU8DDx5mYRPKV7KV5vPblBfAL+TRd9sLaxniQKBgQCusiXZY1QKMCbq888fX1Ywy4Bbg29W7NzjhZTLv4HM7nPlMfGGWAn6Hkchp5W8ZjyHLo4oK7lm6wmhRmXkJpmBjEDRFs1iUFTNscslqwrfVpjjOHCsMAD4BYNV3qYAfCXNhEoai6c5A4F8JD4jTj1aFiMjn9uW2qpFOQn4hX9duwKBgQC1AsmP21rKmx1x4qilgMlgiICtoynIiMClULiU0ASWVKL4WVqCU9mPH9M5T8vx24br0s7vxnKjZi8acir4AJ6CUO+dfzhHmNPNuHqPgsHGVVgPp8F6vJGbQftHp3uZyjej66PmZvA9+JCHRlGpZUh5cOj9ysaTC+iv5vV+QNJjOQKBgFoEfjBYWHOvgD5NR4sY6ybVldySC4s1HlU2/gdHNTFOJfYSGCxrrShu9aRH4IZmAAaAXMwiS7TWo+2wgsxff3YbuUKEh2yDjE85sD5GtqGC0+Exsg1n5vfmj1huFAfn8OrHog+vqolcXxqC6mny5PZIte6jbtl674Yf8B1ZXNoFAoGARxf90ctk0fiJ5dn6KkPsTTYkMCtr7wrltuS7N6F25Y8VF5c+Bi2/rqxD9TWyGlZbP+86QoXjIoMhk6oQjCx4duFI8OlXN9aTwbXV6IdVxoKr7s1kKW4bJS2h8g7NVcfE0+Ay9bAKWEQt+MkPdZZ5vZY+rRNmA4dYdGYubIkfqXY=";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEApxvzTvOu/jMSN89bxTCWrWdpOtcypCj8Y28OxIBfYRfY2naSbfGxSlnQoK0Yu7gtbC54plM7FWPM5LkJGYoSfWp/zsW7Gj0RXeSC8Hnim1lA5m1BkIvJlzjE4IrOL1cW+JrJXJMtqTOjVVK1c6HprF5JOMSIATu4uUCEObNcALd5Nszo7fUMsgTgyRODBu5oGVEuSZWwbRHTkyyVQjfNZHgLrrOZM2Z7yjo7c2F2sj0vf/2392SWFFGEg53TovgAGWK0jHDEY5AdeQc4Snlh/5YwxdIuN1TsVBfEzfEOaGTm/l1rit7AXo0RPLjnhbMjcyNSusn1Y8+0u6Kpo4wJaQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url = "http://497n86m7k7.52http.net/payed/notify";

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://order.toymail.com/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    private String timeout = "30m";

    private String format = "30m";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();
        AlipayTradePayModel model = new AlipayTradePayModel();
        model.setOutTradeNo(out_trade_no);
        model.setProductCode("FAST_INSTANT_TRADE_PAY");
        model.setTotalAmount(total_amount);
        model.setSubject(subject);
        model.setBody(vo.getBody());
        model.setTimeoutExpress(timeout);
        alipayRequest.setBizModel(model);

        String result = alipayClient.pageExecute(alipayRequest).getBody();


//        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
//                + "\"total_amount\":\""+ total_amount +"\","
//                + "\"subject\":\""+ subject +"\","
//                + "\"body\":\""+ body +"\","
//                + "\"timeout_express\":\""+timeout+"\","
//                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");
//
//        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
